// list-driver.ts - 司机端任务列表页
interface Task {
  id: string
  type: string
  typeIcon: string
  typeName: string
  startLocation: string
  startAddress?: string
  endLocation: string
  endAddress?: string
  status: string
  statusText: string
  urgencyLevel: string
  urgencyText: string
  passengerCount: number
  createTime: string
}

Page({
  data: {
    statusBarHeight: '44px',
    navbarHeight: '132rpx',
    contentPaddingTop: '232rpx',
    currentTab: 'all',
    tabs: [
      { id: 'all', name: '全部任务' },
      { id: 'pending', name: '未完成' },
      { id: 'completed', name: '已完成' }
    ],
    allTasks: [] as Task[],
    filteredTasks: [] as Task[],
    loading: false
  },

  onLoad() {
    this.setStatusBarHeight()
    this.checkLogin()
    this.loadTasks()
  },

  onShow() {
    this.loadTasks()
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().updateSelected(0)
    }
  },

  setStatusBarHeight() {
    const windowInfo = (wx as any).getWindowInfo()
    const statusBarHeight = windowInfo.statusBarHeight || 44
    const navbarContentHeight = 88 // rpx
    
    // 计算导航栏总高度（状态栏 + 导航栏内容）
    // 将 px 转换为 rpx：px * 2 (在 375px 宽度屏幕上)
    const statusBarHeightRpx = statusBarHeight * 2
    const navbarTotalHeight = statusBarHeightRpx + navbarContentHeight
    const tabsHeight = 100 // rpx
    const contentPaddingTop = navbarTotalHeight + tabsHeight
    
    this.setData({
      statusBarHeight: statusBarHeight + 'px',
      navbarHeight: navbarTotalHeight + 'rpx',
      contentPaddingTop: contentPaddingTop + 'rpx'
    })
  },

  checkLogin() {
    const userRole = wx.getStorageSync('userRole')
    if (userRole !== 'driver') {
      wx.reLaunch({ url: '/pages/login/login' })
    }
  },

  switchTab(e: any) {
    const tabId = e.currentTarget.dataset.id
    this.setData({ currentTab: tabId })
    this.applyFilters()
  },

  loadTasks() {
    this.setData({ loading: true })
    setTimeout(() => {
      const mockTasks: Task[] = [
        {
          id: '001',
          type: 'ambulance',
          typeIcon: '🚑',
          typeName: '救护车',
          startLocation: '辽宁省-沈阳市',
          startAddress: '某某东大街112号院东恒生宇楼B座11.5',
          endLocation: '北京市-东城区',
          endAddress: '协和医院本部',
          status: 'pending',
          statusText: '待接单',
          urgencyLevel: 'low',
          urgencyText: '低',
          passengerCount: 2,
          createTime: '2025-09-07 周日 09:22'
        },
        {
          id: '002',
          type: 'ambulance',
          typeIcon: '🚑',
          typeName: '救护车',
          startLocation: '辽宁省-沈阳市',
          startAddress: '某某东大街112号院东恒生宇楼B座11.5',
          endLocation: '北京市-东城区',
          endAddress: '协和医院本部',
          status: 'in_progress',
          statusText: '运输中',
          urgencyLevel: 'high',
          urgencyText: '高',
          passengerCount: 1,
          createTime: '2025-09-07 周日 09:22'
        },
        {
          id: '003',
          type: 'ambulance',
          typeIcon: '🚑',
          typeName: '救护车',
          startLocation: '辽宁省-沈阳市',
          startAddress: '某某东大街112号院东恒生宇楼B座11.5',
          endLocation: '北京市-东城区',
          endAddress: '协和医院本部',
          status: 'completed',
          statusText: '已完成',
          urgencyLevel: 'medium',
          urgencyText: '中',
          passengerCount: 3,
          createTime: '2025-09-07 周日 09:22'
        }
      ]
      
      this.setData({
        allTasks: mockTasks,
        loading: false
      })
      this.applyFilters()
    }, 800)
  },

  applyFilters() {
    const { allTasks, currentTab } = this.data
    let filtered = [...allTasks]
    
    if (currentTab === 'pending') {
      filtered = filtered.filter(task => task.status === 'pending' || task.status === 'in_progress')
    } else if (currentTab === 'completed') {
      filtered = filtered.filter(task => task.status === 'completed')
    }
    
    this.setData({ filteredTasks: filtered })
  },

  viewDetail(e: any) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/detail-driver/detail-driver?id=${id}`
    })
  },

  showMore() {
    wx.showActionSheet({
      itemList: ['刷新列表', '查看路线', '联系调度中心'],
      success: (res) => {
        switch(res.tapIndex) {
          case 0:
            this.loadTasks()
            wx.showToast({ title: '已刷新', icon: 'success' })
            break
          case 1:
            wx.showToast({ title: '功能开发中', icon: 'none' })
            break
          case 2:
            wx.makePhoneCall({ phoneNumber: '400-123-4567' })
            break
        }
      }
    })
  }
})

